/**
 *  查找元素节点上的祖先节点，如果祖先节点包含指定类名，则返回该祖先节点
 *  @param ele  元素节点
 *  @param classes 类名数组
 */
export const findNodeUpperByClasses = (
  ele: Node | null,
  classes: string[],
): HTMLElement | undefined => {
  if (!ele || !(ele instanceof Element)) {
    // 节点不存在或者不是元素节点，停止递归
    return undefined;
  }

  const parentNode = ele.parentNode;

  if (parentNode && parentNode instanceof Element) {
    const classList = parentNode.classList;
    const hasAllClasses = classes.every((className) => classList.contains(className));
    if (hasAllClasses) {
      return parentNode as HTMLElement;
    } else {
      // 递归向上查找
      return findNodeUpperByClasses(parentNode, classes);
    }
  } else {
    return undefined;
  }
};
